package com.cvit.controller;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.dao.OrdersMapper;
import com.cvit.model.Orders;
import com.cvit.model.Student;
import com.cvit.service.OrdersService;
import com.cvit.service.StudentService;
import com.cvit.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import freemarker.core._RegexBuiltins.split_reBI;

@Controller
public class OrdersController {
	
	@Autowired
	OrdersService ordersService;
	@Autowired
	StudentService studentService;
	
	/**
	 * 查询所有
	 * */
	@RequestMapping("/getOrders")
	@ResponseBody
	public Msg getOrders(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn, 10);
		List<Orders> orders = ordersService.getAll();
		PageInfo<Orders> page = new PageInfo<Orders>(orders, 10);
		return Msg.success().add("pageInfo", page);
	}
	
	
	
	/**
	 * 根据考生号、身份证号查询
	 * */
	@RequestMapping("/getOrdersBys")
	@ResponseBody
	public Msg getStudentBys(Orders orders){
		List<Orders> ordersL = ordersService.getOrdersBys(orders);
		return Msg.success().add("Orders", ordersL);
	}
	
	/**
	 * 修改缴费状态(未缴费到缴费)
	 * updatepayTypeById
	 * */
	@RequestMapping("/ordersUpdatepayType")
	@ResponseBody
	public Msg updatepayTypeById(@RequestParam(value = "id")String id){
		ordersService.updatePayType(id);
		return Msg.success();
	}
	
	/**
	 * 凭证（根据学生表Id）
	 * */
	@RequestMapping("/getVoucher")
	@ResponseBody
	public Msg getVoucher(@RequestParam(value = "studentId")String studentId){
		Orders orders = ordersService.getVoucher(studentId);
		return Msg.success().add("Orders", orders);
	}
	
	/**
	 * 修改测试
	 * */
	@RequestMapping("/uptest")
	@ResponseBody
	public Msg uptest(@RequestParam(value = "dd")String dd){
		String a=ordersService.selectOpenid(dd);
		int b=ordersService.updatePayType(dd);
		studentService.updatePayType(a);
		return Msg.success().add("Orders", a+"///////"+b);
	}
	
	/**
	 * 根据学校名称和专业名称查询
	 * */
	@RequestMapping("/selectOrdersBySchoolAndMajor")
	@ResponseBody
	public Msg selectOrdersBySchoolAndMajor(@RequestParam(value = "pn", defaultValue = "1") Integer pn,Orders order){
		PageHelper.startPage(pn, 5);
		List<Orders> orders = ordersService.selectOrdersBySchoolAndMajor(order);
		PageInfo<Orders> page = new PageInfo<Orders>(orders, 5);
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * 导出Excel
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping("/OrdersExport")  
	public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, UnsupportedEncodingException {  
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
		String ordersData = df.format(new Date());
	    if(ordersData!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setHeader("Content-Disposition", "attachment;filename="  
			        + new String(ordersData.getBytes(),"iso-8859-1") + ".xlsx");
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象  
	        workbook = ordersService.exportExcelInfo(ordersData);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
	
	/**
	 * 导出Excel-SchoolAndMajor(school和major可以为空)
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping("/OrdersExportSchoolAndMajor")  
	public @ResponseBody void exportSchoolAndMajor(HttpServletRequest request, HttpServletResponse response, Orders orders) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, UnsupportedEncodingException {  
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");//设置日期格式
		String ordersData = df.format(new Date());
	    if(ordersData!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setHeader("Content-Disposition", "attachment;filename="  
			        + new String(ordersData.getBytes(),"iso-8859-1") + ".xlsx");
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象
	        workbook = ordersService.exportExcelInfoBySchoolAndMajor(ordersData,orders);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
}
